<template>
  <el-form style="width: 40%" :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">

    <el-form-item label="姓名" prop="name">
      <el-input v-model="ruleForm.name"></el-input>
    </el-form-item>
    <el-form-item label="性别" prop="nex">
      <el-input v-model="ruleForm.nex"></el-input>
    </el-form-item>
    <el-form-item label="系别" prop="step">
      <el-input v-model="ruleForm.step"></el-input>
    </el-form-item>
    <el-form-item label="病例" prop="ill">
      <el-input v-model="ruleForm.ill"></el-input>
    </el-form-item>
    <el-form-item label="日期" prop="data">
      <el-input v-model="ruleForm.data"></el-input>
    </el-form-item>

    <el-form-item>
      <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
      <el-button @click="resetForm('ruleForm')">重置</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
export default {
  data() {
    return {
      ruleForm: {
        name: '',
        nex: '',
        step: '',
        ill: '',
        data: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入姓名', trigger: 'blur' },
          { min: 1, max: 8, message: '长度在 1 到 8 个字符', trigger: 'blur' }
        ],
        nex: [
          { required: true, message: '请输入性别', trigger: 'blur' },
          { min: 1, max: 8, message: '长度在 1 到 8 个字符', trigger: 'blur' }
        ],
        step: [
          { required: true, message: '请输入系别', trigger: 'blur' },
          { min: 1, max: 8, message: '长度在 1 到 8 个字符', trigger: 'blur' }
        ],
        age: [
          { required: true, message: '请输入病例', trigger: 'blur' },
          { min: 1, max: 8, message: '长度在 1 到 8 个字符', trigger: 'blur' }
        ],
        data: [
          { required: true, message: '请输入日期', trigger: 'blur' },
          { min: 1, max: 8, message: '长度在 1 到 8 个字符', trigger: 'blur' }
        ]

      }
    };
  },
  methods: {
    submitForm(formName) {
      const _this = this
      this.$refs[formName].validate((valid) => {
        if (valid) {
          axios.post('http://localhost:8181/healthy/save',this.ruleForm).then(function (resp) {
            if(resp.data == 'success'){
              _this.$alert('《'+_this.ruleForm.name+'》添加成功', '消息', {
                confirmButtonText: '确定',
                callback: action => {
                  _this.$router.push('/bingLi')
                }
              });
              //_this.$message('添加成功')
            }
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>
